-
-
Notifications
You must be signed in to change notification settings - Fork 988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Find
to Routes
interface
#872
Add Find
to Routes
interface
#872
Conversation
That's cool feature 💯 |
hi @joeriddles what will be the official middleware repo for your package? |
Thanks for the review @pkieltyka! I have renamed the file and variables you requested in this commit: 5cf9bc3
Do you mean I should add a new middleware file, like |
@pkieltyka I have added a new middleware here: f6c1df8. I also updated the example I added to use the new |
👀 🙌🏾 |
any news? |
@pkieltyka any chance this gets merged soon? |
I am going to fork the repo and merge this PR in the fork 👀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
The .Find()
method on Mux is indeed a very nice contribution to chi. LGTM. Thank you!
But for now, I removed the middleware.FindRoute()
in 1daabdc, as I think it needs some more thoughts / discussion.
r.Use(middleware.FindPattern(r, func(pattern string) {
fmt.Printf("pattern=%s\n", pattern)
}))
I think it'd make sense to rename this to middleware.RoutePattern()
but also make the *http.Request
and http.ResponseWriter
available to the callback, so users could check r.Method
/r.Host
/... and for example respond with HTTP 403
if they were to implement ACL inside of this callback.
If you have good ideas how to finish this new middleware API, please submit an issue with a proposal first.
Thank you for your contribution!
@VojtechVitek Can you make a new release please? |
Thanks for merging @VojtechVitek! |
@joeriddles Is it expected behaviour? It seems to truncate the actual path: m := chi.NewMux()
m.Route("/yo", func(r chi.Router) {
r.Get("/sup", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("sup"))
})
})
fmt.Println(m.Find(chi.NewRouteContext(), http.MethodGet, "/yo/sup")) // "/sup", but expected "/yo/sup" |
btw: I have just found out that we already have this
Isn't it what we were after? |
No, that is not expected behavior. I will create a fix. |
The goal of See the issue description:
|
The issue with RoutePattern is that it returns the pattern only if the request successfully reaches the route see here. However, in some cases (such as collecting metrics), it's necessary to determine the route pattern before the request is fully executed. This is important because requests can fail or be declined by authentication middleware. |
Cool, makes sense. Thanks for the explanation 👍 @joeriddles I'll hold off with releasing new minor version until the fix is ready. Please keep me posted. |
@joeriddles any updates? This is blocking the release, so I'm tempted to revert this PR until a fix for #872 (comment) is ready. Thoughts? |
@VojtechVitek a follow-up PR is created here: #954. Thanks! |
Do you have any updates on when we might expect the release? |
Background
I am working on adding a middleware for OpenTelemetry to a Chi-based service that needs to send a metric before the request has executed. We need to include a string for the API endpoint, like the route pattern, in the metric. This is currently not possible due to the way request route patterns are resolved using Chi's trie-based router, as noted by the maintainer in this issue #692 (comment).
Description
This PR proposes adding a
Find
function to theRoutes
interface. ThisFind
method closely mirrors theMatch
function that already exists. UnlikeMatch
, which returns a boolean value if a route exists for the path/HTTP method combination,Find
returns the pattern that was matched.Unit tests have been added for
Find
, as well as new tests added forMatch
. I refactoredMatch
to useFind
under the hood, since they are both doing almost the same thing.This PR also adds a new middleware,
FindPattern
, which allows the user to pass a callback function that will receive the fully resolved route pattern from the HTTP request.Examples
A basic example of using
Find
in a middleware before the request is handled can be found here: https://github.com/joeriddles/chi-api-middleware, specifically this function https://github.com/joeriddles/chi-api-middleware/blob/main/main.go#L37.A smaller example has also been added to the
_examples
folder.